package qmm.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import qmm.entity.User;
import qmm.util.JdbcUtil;

@MultipartConfig
@WebServlet("/FaceUploadServlet")
public class FaceUploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public FaceUploadServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");	
		Part part = request.getPart("face");
		String filename = part.getSubmittedFileName();
		String path = request.getServletContext().getRealPath("/upload/"+filename);
		System.out.println(path);
		part.write(path);
		String sql = "update t_user set face = '"+filename+"' where id = "+user.getId();
		int n = JdbcUtil.update(sql);
		if(n>0){
			user.setFace(filename);
			session.setAttribute("user", user);
		}
		response.sendRedirect("member_user.jsp");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
